import { defineStore } from 'pinia'
import { ref } from 'vue'

const STORAGE_KEY = 'user_info'

export const useUserStore = defineStore('user', () => {
  // 从localStorage初始化状态
  const storedUser = JSON.parse(localStorage.getItem(STORAGE_KEY) || '{}')
  const id = ref(storedUser.id || 0)
  const email = ref(storedUser.email || '')
  const token = ref(storedUser.token || '')
  const username = ref(storedUser.username || '')
  const uid = ref(storedUser.uid || '')
  const avatar = ref(storedUser.avatar || '')
  const age = ref(storedUser.age || 0)
  const address = ref(storedUser.address || '')
  const university = ref(storedUser.university || '')
  const signature = ref(storedUser.signature || '')
  const likeCount = ref(storedUser.likeCount || 0)
  const commonConcern = ref(storedUser.commonConcern || 0)
  const concernCount = ref(storedUser.concernCount || 0)
  const vermicelliCount = ref(storedUser.vermicelliCount || 0)
  const favoriteCount = ref(storedUser.favoriteCount || 0)
  const articleCount = ref(storedUser.articleCount || 0)

  const setUserInfo = (userInfo) => {
    id.value = userInfo.id
    username.value = userInfo.username
    email.value = userInfo.email
    uid.value = userInfo.uid
    avatar.value = userInfo.avatar
    age.value = userInfo.age
    address.value = userInfo.address
    university.value = userInfo.university
    signature.value = userInfo.signature
    likeCount.value = userInfo.likeCount
    commonConcern.value = userInfo.commonConcern
    concernCount.value = userInfo.concernCount
    vermicelliCount.value = userInfo.vermicelliCount
    favoriteCount.value = userInfo.favoriteCount
    articleCount.value = userInfo.articleCount

    // 保存到localStorage
    localStorage.setItem(STORAGE_KEY, JSON.stringify({
      id: id.value,
      token: token.value,
      username: username.value,
      uid: uid.value,
      email: email.value,
      avatar: avatar.value,
      age: age.value,
      address: address.value,
      university: university.value,
      signature: signature.value,
      likeCount: likeCount.value,
      commonConcern: commonConcern.value,
      concernCount: concernCount.value,
      vermicelliCount: vermicelliCount.value,
      favoriteCount: favoriteCount.value,
      articleCount: articleCount.value
    }))
  }

  const setToken = (newToken) => {
    token.value = newToken
    // 更新localStorage中的token
    const storedData = JSON.parse(localStorage.getItem(STORAGE_KEY) || '{}')
    storedData.token = newToken
    localStorage.setItem(STORAGE_KEY, JSON.stringify(storedData))
  }

  const clearUserInfo = () => {
    id.value = 0
    email.value = ''
    token.value = ''
    username.value = ''
    uid.value = ''
    avatar.value = ''
    age.value = 0
    address.value = ''
    university.value = ''
    signature.value = ''
    likeCount.value = 0
    commonConcern.value = 0
    concernCount.value = 0
    vermicelliCount.value = 0
    favoriteCount.value = 0
    articleCount.value = 0
    // 清除localStorage
    localStorage.removeItem(STORAGE_KEY)
  }

  return {
    id,
    email,
    token,
    username,
    uid,
    avatar,
    age,
    address,
    university,
    signature,
    likeCount,
    commonConcern,
    concernCount,
    vermicelliCount,
    favoriteCount,
    articleCount,
    setUserInfo,
    setToken,
    clearUserInfo
  }
}) 